package com.personal.kousuan.db;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import com.personal.kousuan.MainFragmentActivity;

@SuppressLint("UseSparseArrays")
public class CommonData {

	private static CommonData instance;
	private MyDbHelper db;
	public Map<Integer,Semester> semesters = new LinkedHashMap<Integer,Semester>();
	public Map<Integer, Map<String,Category>> categorys = new LinkedHashMap<Integer,Map<String,Category>>();
	public class Semester{
		int id;
		String name;
		public Semester(int id,String name){
			this.id = id;
			this.name = name;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
	}
	
	public class Category{
		int id;
		int semesterId;
		String categoryKey;
		String categoryName;
		public Category(int id, int semesterId, String categoryKey,
				String categoryName) {
			super();
			this.id = id;
			this.semesterId = semesterId;
			this.categoryKey = categoryKey;
			this.categoryName = categoryName;
		}
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public int getSemesterId() {
			return semesterId;
		}
		public void setSemesterId(int semesterId) {
			this.semesterId = semesterId;
		}
		public String getCategoryKey() {
			return categoryKey;
		}
		public void setCategoryKey(String categoryKey) {
			this.categoryKey = categoryKey;
		}
		public String getCategoryName() {
			return categoryName;
		}
		public void setCategoryName(String categoryName) {
			this.categoryName = categoryName;
		}
		
		
	}
	
	public static CommonData getInstance(){
		if (instance == null) {
			instance = new CommonData();
		}
		
		return instance;
	}
	
	public void load(Context context){
		db = MainFragmentActivity.db;
		loadSemester();
		
		categorys.clear();
		Iterator<Integer> it = semesters.keySet().iterator();
		while(it.hasNext()){
			int key = it.next();
			loadCategory(key);
		}
		
		
	}
	
	private void loadSemester(){
		semesters.clear();
		Cursor cursor = db.select(MyDbInfo.getTableNames()[0], MyDbInfo.getFieldNames()[0], null, null, null, null, "ID");
		while(cursor.moveToNext()){
			semesters.put(cursor.getInt(0), new Semester(cursor.getInt(0),cursor.getString(1)));
			//categorys.put(cursor.getInt(0), new HashMap<String,Category>());
		}
		cursor.close();
	}
	
	private void loadCategory(int semester){
		Map<String,Category> map = new LinkedHashMap<String, Category>();
		Cursor cursor = db.select(MyDbInfo.getTableNames()[1], MyDbInfo.getFieldNames()[1], MyDbInfo.getFieldNames()[1][1] + "=" + semester, null, null, null, "ID");
		while(cursor.moveToNext()){
			map.put(cursor.getString(2), new Category(cursor.getInt(0),cursor.getInt(1),cursor.getString(2),cursor.getString(3)));
			
		}
		categorys.put(semester, map);
		cursor.close();
	}
	
}
